#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    vector<string> topKFrequent(vector<string>& words, int k) {
        map<string, int> wordCount;;
        for (auto& e : words)
        {
            wordCount[e]++;
        }

        multimap<int, string, greater<int>> sortWordCount;
        for (auto& e : wordCount)
        {
            sortWordCount.insert(make_pair(e.second, e.first));
        }

        vector<string> ret;
        auto it = sortWordCount.begin();
        while (k--)
        {
            ret.push_back(it->second);
            ++it;
        }

        return ret;
    }
};